package redis

import (
	"context"
	"fmt"
	"muziai-server/global"
	"muziai-server/models/ctype"
	"muziai-server/utils/jwts"
	"time"
)

func (RedisService) SetLogoutToken(logoutype ctype.LogoutType, token string, expires int64) (err error) {

	claims, _ := jwts.ParseToken(token)
	subTime := claims.ExpiresAt - time.Now().Unix()
	key := fmt.Sprintf("logout_%s", token)
	err = global.Redis.SetNX(context.Background(), key, ctype.LogoutType.String(logoutype), time.Duration(subTime)*time.Second).Err()
	return
}

func (RedisService) HasLogoutToken(token string) (val string, ok bool) {
	val, err := global.Redis.Get(context.Background(), fmt.Sprintf("logout_%s", token)).Result()
	if err == nil {
		return val, true
	}
	return
}
